Skip to content

fix(instance): reopening a route while it pops reuses a disposed controller - #3491

Open
MikaelDDavidd wants to merge 1 commit into
jonataslaw:masterfrom
MikaelDDavidd:fix/stale-controller-fast-renavigation-3446
Open

MikaelDDavidd wants to merge 1 commit into
jonataslaw:masterfrom
MikaelDDavidd:fix/stale-controller-fast-renavigation-3446

Conversation

@MikaelDDavidd

@MikaelDDavidd MikaelDDavidd commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #3446
Also looks like the same thing reported in #1880.

If you go back from a page and open the same route again before the pop transition ends, the new page ends up with a controller that is already closed and no longer registered. Anything that touches it after that (its TextEditingController, Get.find, etc.) breaks. If you wait for the transition to finish, it works fine.

Two things cause it, and both need fixing:

  1. GetObserver.didPop doesn't call reportRouteWillDispose for the popped route. didRemove and didReplace already do. Without it the dependency isn't marked dirty, so the binding of the reopened route keeps the old instance instead of registering a new one.
  2. delete() only uses lateRemove when the current entry is dirty. After a dirty entry gets replaced, the new entry is never dirty, so the old route's dispose deletes the new instance (the one the page is using) and the old one is never closed.

I changed didPop to do the same as didRemove, and delete() to use lateRemove whenever it's set. The delete() part is the same change #2421 proposed back in 2022, for a file that no longer exists on master. I tried each change on its own and the new tests still fail with only one of them.

To check it in a real app I built the code from the issue for web with Flutter 3.47.8, once against master and once against this branch. Two changes to the issue code: BindingsBuilder doesn't exist on master anymore so I used a small Bindings class, and I added a line on Page 2 that shows which controller the page holds and whether it's closed. Same sequence on both builds (Ctrl+I, Ctrl+B, Ctrl+I 230ms later), then typing in the field:

master: reopened page gets a closed controller; this PR: it gets a live one

The tests are in test/navigation/reopen_route_during_pop_test.dart. Three fail on master: reopening right away, reopening 150ms into the pop, and checking that the reopened page's controller is deleted when that page closes (so nothing leaks). The fourth checks that a normal pop still deletes the controller.

All 226 tests pass on Flutter 3.47.8 and 3.50.0.

Pre-launch Checklist

  • I updated/added relevant documentation (doc comments with ///). No public API changes.
  • I added new tests to check the change I am making or feature I am adding, or @jonataslaw said the PR is test-exempt.
  • All existing and new tests are passing.

…roller

Going back and opening the same route again before the pop transition
finished left the new page with the old controller. The old route's
dispose then deleted the controller the new page was using, so things
like its TextEditingController failed after dispose.

Two things caused it:

- GetObserver.didPop didn't mark the popped route's dependencies as
  dirty (didRemove and didReplace already do), so the binding of the
  reopened route kept the old instance instead of registering a new one.
- delete() only picked the replaced instance (lateRemove) when the
  current entry was dirty, which is never the case after a replacement.
  It deleted the new instance and leaked the old one.

Fixes jonataslaw#3446

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I get errors when quickly navigating back and forth between pages using keyboard shortcuts

1 participant